// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    categories: [
      { title: '二手书', desc: '二手书', bg: '#ffb48a', icon: 'bookmark' },
      { title: '数码', desc: '数码电器', bg: '#b6aaff', icon: 'mobile' },
      { title: '服饰', desc: '男女服饰', bg: '#ffb4b4', icon: 'clothes' },
      { title: '二手车', desc: '二手车', bg: '#7be495', icon: 'car' },
      { title: '二手房', desc: '二手房', bg: '#7be4c7', icon: 'home' },
      { title: '热租宝贝', desc: '充值缴费', bg: '#4dbfff', icon: 'fire' },
      { title: '极速回收', desc: '极速回收', bg: '#ff7b7b', icon: 'recycle' },
      { title: '神券', desc: '神券', bg: '#ffb48a', icon: 'gift' },
      { title: '借钱', desc: '借钱', bg: '#ff7b7b', icon: 'red-packet' },
      { title: '会员', desc: 'PLUS会员', bg: '#2d3a8c', icon: 'vip' }
    ],
    hotItems: [
      { name: '女士手提包', img: '/pages/index/1.png' },
      { name: '大疆pocket 3', img: '/pages/index/2.png' },
      { name: '露营帐篷', img: '/pages/index/3.png' },
      { name: '折叠小车', img: '/pages/index/4.png' },
      { name: '小型家用制冰机', img: '/pages/index/5.png' },
      { name: '金手镯', img: '/pages/index/6.png' }
    ]
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  goCategory() {
    wx.redirectTo({ url: '/pages/category/index' })
  },
  onCategoryTap(e) {
    const idx = e.currentTarget.dataset.index;
    const item = this.data.categories[idx];
    if (item.title === '二手书') {
      wx.navigateTo({ url: '/pages/book-category/index' });
    } else if (item.title === '数码') {
      wx.navigateTo({ url: '/pages/digital-category/index' });
    } else if (item.title === '神券') {
      wx.navigateTo({ url: '/pages/coupon-list/index' });
    } else {
      wx.showToast({ title: '功能开发中', icon: 'none' });
    }
  },
  goSearch() {
    wx.navigateTo({ url: '/pages/search/search' });
  },
  goMine() {
    wx.redirectTo({
      url: '/pages/mine/index'
    })
  },
  goHome() {
    wx.redirectTo({ url: '/pages/index/index' })
  },
  // 热门单品点击事件
  onHotItemTap(e) {
    // 可根据实际数据传递id
    wx.navigateTo({ url: '/pages/goodsDetail/goodsDetail' });
  },
})
